package com.stu.vertx.hello.shiro;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class CryRealm extends AuthorizingRealm {
    @Override
    public String getName() {
        return "CryptRealm";
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authToken) throws AuthenticationException {

        UsernamePasswordToken token = (UsernamePasswordToken) authToken;

        // 用户输入的
        String username = token.getUsername();
        String password = new String(token.getPassword());

        System.out.println("用户输入的用户名: ");

        // 模拟数据库中的密码(密文)
        String dbPassword = "8eda1535b635e6b2c3672cc502249fe0";

        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(
                username,                                       // 用户名
                dbPassword,                                     // 密码(数据库中的)
                ByteSource.Util.bytes("123"),            		// 盐
                getName()                                       // realm的名称
        );
        return info;
    }
}